﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Usuario
''' </summary>
''' <remarks></remarks>
Public Class DALUsuario

    Private Shared m_Instance As DALUsuario
    Private Shared m_Semaforo As New Object
    Private m_TDGUsuario As TDGUsuario
    Private m_TDGFamilia As TDGFamilia


    Public Function Insert(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Insert(database.Conexion, obj.Usu_Legajo, obj.Usu_Nombre, obj.Usu_Apellido, _
                                             obj.Idioma.Idi_Codigo, obj.DVH)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloAlta(ex)
        End Try

    End Function

    Public Function Update(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Update(database.Conexion, obj.Usu_Legajo, obj.Usu_Nombre, obj.Usu_Apellido, _
                                             obj.Idioma.Idi_Codigo, obj.Estado_Usuario.EstU_Codigo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloEditar(ex)
        End Try
    End Function

    Public Function Baja(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Update_Baja(database.Conexion, obj.Usu_Legajo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloBaja(ex)
        End Try
    End Function

    Public Function CambiarIdioma(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Update_Idioma(database.Conexion, obj.Usu_Legajo, obj.Idioma.Idi_Codigo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloEditar(ex)
        End Try
    End Function

    Public Function Estado_Bloquear(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Update_EstadoBloquear(database.Conexion, obj.Usu_Legajo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioEstadoBloquear(ex)
        End Try
    End Function

    Public Function Estado_Activar(obj As cUsuario) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Update_EstadoActivar(database.Conexion, obj.Usu_Legajo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioEstadoActivar(ex)
        End Try
    End Function

    Public Function Sel(obj As cUsuario) As cUsuario
        Dim dr As SqlDataReader
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGUsuario.Find(database.Conexion, obj.Usu_Legajo)

            If dr.HasRows Then

                dr.Read()

                obj.Usu_Legajo = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.Usu_Nombre = Utils.Convert.ToStr(dr.GetValue(1))
                obj.Usu_Apellido = Utils.Convert.ToStr(dr.GetValue(2))
                obj.Idioma.Idi_Codigo = Utils.Convert.ToInt16(dr.GetValue(3))
                obj.Estado_Usuario.EstU_Codigo = Utils.Convert.ToStr(dr.GetValue(4))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(5))
                obj.Idioma.Idi_Nombre = Utils.Convert.ToStr(dr.GetValue(6))
                obj.Estado_Usuario.EstU_Descrip = Utils.Convert.ToStr(dr.GetValue(7))
                obj.Estado_Usuario.EstU_AccedeAlSistema = Utils.Convert.ToBool(dr.GetValue(8))


                dr.Close()
            Else
                obj = Nothing
            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloBuscar(ex)
        End Try
    End Function

    Public Function SelectAll() As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cUsuario
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGUsuario.FindAll(database.Conexion)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cUsuario

                    objAux.Usu_Legajo = Utils.Convert.ToInt16(dr.GetInt16(0))
                    objAux.Usu_Nombre = Utils.Convert.ToStr(dr.GetString(1))
                    objAux.Usu_Apellido = Utils.Convert.ToStr(dr.GetString(2))
                    objAux.Idioma.Idi_Codigo = Utils.Convert.ToInt16(dr.GetInt16(3))
                    objAux.Estado_Usuario.EstU_Codigo = Utils.Convert.ToStr(dr.GetString(4))
                    objAux.DVH = 0
                    objAux.Idioma.Idi_Nombre = Utils.Convert.ToStr(dr.GetString(6))
                    objAux.Estado_Usuario.EstU_Descrip = Utils.Convert.ToStr(dr.GetString(7))
                    objAux.Estado_Usuario.EstU_AccedeAlSistema = Utils.Convert.ToBool(dr.GetBoolean(8))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloConsultar(ex)
        End Try
    End Function

    Public Function Select_Patentes(Usu_Legajo As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cPatente
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGUsuario.Find_Patentes(database.Conexion, Usu_Legajo)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cPatente

                    objAux.Pat_Codigo = Utils.Convert.ToInt16(dr.GetInt16(0))
                    objAux.Pat_Descripcion = Utils.Convert.ToStr(dr.GetString(1))
                    objAux.Checked = Utils.Convert.ToBool(dr.GetValue(2))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloConsultarPatentes(ex)
        End Try

    End Function

    Public Function Select_Familias(Usu_Legajo As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cFamilia
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGUsuario.Find_Familias(database.Conexion, Usu_Legajo)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cFamilia

                    objAux.Fam_Codigo = Utils.Convert.ToInt16(dr.GetInt16(0))
                    objAux.Fam_Descrip = Utils.Convert.ToStr(dr.GetString(1))
                    objAux.Checked = Utils.Convert.ToBool(dr.GetValue(2))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloConsultar(ex)
        End Try

    End Function

    Public Function Insert_Patente(objUsuario As cUsuario, objPatente As cPatente) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Insert_Patente(database.Conexion, objUsuario.Usu_Legajo, objPatente.Pat_Codigo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloAlta(ex)
        End Try

    End Function

    Public Function Insert_Familia(objUsuario As cUsuario, objFamilia As cFamilia) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Insert_Familia(database.Conexion, objUsuario.Usu_Legajo, objFamilia.Fam_Codigo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloAlta(ex)
        End Try

    End Function

    Public Function Delete_Patente(objUsuario As cUsuario, objPatente As cPatente) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Delete_Patente(database.Conexion, objUsuario.Usu_Legajo, objPatente.Pat_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloBajaAcceso(ex)
        End Try
    End Function

    Public Function Delete_Familia(objUsuario As cUsuario, objFamilia As cFamilia) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGUsuario.Delete_Familia(database.Conexion, objUsuario.Usu_Legajo, objFamilia.Fam_Codigo)
            database.Desconectar()

            Return (iResultado > 0)

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloBajaAcceso(ex)
        End Try
    End Function

    Public Function TienePermiso(oUsuario As cUsuario, oPatente As cPatente) As Boolean
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            TienePermiso = m_TDGUsuario.TienePermiso(database.Conexion, oUsuario.Usu_Legajo, oPatente.Pat_Codigo)
            database.Desconectar()

        Catch ex As Exception
            Throw New Excepciones.UsuarioFalloConsultaTieneAcceso(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALUsuario
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALUsuario
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGUsuario = TDGUsuario.Instance()
        m_TDGFamilia = TDGFamilia.Instance()
    End Sub

End Class
